package com.xingdata.jjxc.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.util.Log;

import com.xingdata.jjxc.activity.VideoPlayActivity;

/**
 * 
 * @Description: TODO(视频工具类)
 * @date 2015-10-24 下午12:06:59 Creater JiangMin
 */
public class VideoTools {
	private static String TAG = "VideoTools";
	private static int Thumbnail_With = 1280;
	private static int Thumbnail_Hight = 720;

	// /**
	// * 获取视频缩略图
	// *
	// * @param url
	// * @param width
	// * @param height
	// * @return
	// */
	// public static Bitmap createVideoThumbnail(String url, int width, int
	// height) {
	// // url="http://img.irishud.com.cn/test";
	// Bitmap bitmap = null;
	// if (url != null && url.length() != 0) {
	// MediaMetadataRetriever retriever = new MediaMetadataRetriever();
	// try {
	// retriever.setDataSource(url, new HashMap<String, String>());
	// bitmap = retriever.getFrameAtTime();
	// } catch (IllegalArgumentException ex) {
	// } catch (RuntimeException ex) {
	// } finally {
	// try {
	// retriever.release();
	// } catch (RuntimeException ex) {
	// }
	// }
	// if (bitmap != null) {
	// bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
	// ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
	// }
	// }
	//
	// return bitmap;
	//
	// }

	/**
	 * 获取网络视频缩略图并且保存到本地
	 * 
	 * @param url
	 * @param width
	 * @param height
	 * @return
	 */
	public static Bitmap createNetVideoThumbnail(String url) {
		Bitmap bitmap = null;
		MediaMetadataRetriever retriever = new MediaMetadataRetriever();
		int kind = MediaStore.Video.Thumbnails.MINI_KIND;
		try {
			if (Build.VERSION.SDK_INT >= 12) {
				retriever.setDataSource(url, new HashMap<String, String>());
			} else {
				retriever.setDataSource(url);
			}
			bitmap = retriever.getFrameAtTime();
		} catch (IllegalArgumentException ex) {
			// Assume this is a corrupt video file
		} catch (RuntimeException ex) {
			// Assume this is a corrupt video file.
		} finally {
			try {
				retriever.release();
			} catch (RuntimeException ex) {
				// Ignore failures while cleaning up.
			}
		}
		if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {
			bitmap = ThumbnailUtils.extractThumbnail(bitmap, Thumbnail_With,
					Thumbnail_Hight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

		}

		// 下载完之后将图标保存到SD卡
		if (bitmap != null && !bitmap.isRecycled()) {
			String mUrl = url.replaceAll(".mp4", ".jpg");
			SDCardTools.SaveUrlImage(mUrl, bitmap);
			bitmap.recycle();
			String imageUrl = SDCardTools
					.isCacheFileIsExit(SDCardTools.PRODUCTS_IMAGE_NAME)
					+ "/"
					+ ImageTools.changeImageName(mUrl);
			return ImageTools.compressImageFromFile(imageUrl, 4.0f);
		}
		
		return null;
	}

	/**
	 * 获取本地视频视频缩略图
	 * 
	 * @param url
	 * @param width
	 * @param height
	 * @return
	 */
	public static Bitmap createVideoThumbnail(String url) {
		Bitmap bitmap = null;
		File file = new File(url);// 实例化一个File
		if (file.exists()) {
			bitmap = ThumbnailUtils.createVideoThumbnail(
					file.getAbsolutePath(), Thumbnails.MICRO_KIND);// 创建一个视频缩略图
			bitmap = ThumbnailUtils.extractThumbnail(bitmap, Thumbnail_With,
					Thumbnail_Hight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);// 指定视频缩略图的大小

			if (bitmap != null && !bitmap.isRecycled()) {
				String mUrl = url.replaceAll(".mp4", ".jpg");
				SDCardTools.SaveUrlImage(mUrl, bitmap);
				bitmap.recycle();
				String imageUrl = SDCardTools
						.isCacheFileIsExit(SDCardTools.PRODUCTS_IMAGE_NAME)
						+ "/" + ImageTools.changeImageName(mUrl);
				return ImageTools.compressImageFromFile(imageUrl, 2.0f);
			}

		} else {
			Log.e(TAG, "bitmap is null!");
		}

		return bitmap;

	}

	/**
	 * 将图片转换为缩略图
	 * 
	 * @param bmp
	 * @return
	 */
	public static byte[] Bitmap2Byte(Bitmap bmp) {
		byte[] b = null;
		ByteArrayOutputStream output = new ByteArrayOutputStream();// 初始化一个流对象
		bmp.compress(CompressFormat.PNG, 100, output);// 把bitmap100%高质量压缩 到
		bmp.recycle();// 自由选择是否进行回收
		b = output.toByteArray();
		try {
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}

	public static void palyVideo(Context context, String strUrl) {
		// if (strUrl != null) {
		// Intent intent = new Intent(Intent.ACTION_VIEW);
		// intent.setDataAndType(Uri.parse(strUrl), "video/*");
		// context.startActivity(intent);
		// }
		if (StringUtil.isStringNull(strUrl)) {
			Intent intent = new Intent();
			intent.setClass(context, VideoPlayActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("url", strUrl);
			intent.putExtras(bundle);
			context.startActivity(intent);

		}

	}

	public static File getFileFromBytes(byte[] b, String outputFile) {
		BufferedOutputStream stream = null;
		File file = null;
		try {
			file = new File(outputFile);
			FileOutputStream fstream = new FileOutputStream(file);
			stream = new BufferedOutputStream(fstream);
			stream.write(b);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return file;
	}

}
